﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibarayMS
{
    public partial class Frm_borrow_manage : Form
    {
        private int brid;
        private string mname,mid;
        public event myDelegate returnEvent;
        public event myDelegate logoutEvent;
        public Frm_borrow_manage(string mname,string mid)
        {
            InitializeComponent();
            this.mname = mname;
            this.mid = mid;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            returnEvent();
            this.Close();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            logoutEvent();
            this.Close();
        }

        protected void bind_book_info()
        {
            MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
            conn.Open();
            string str = "select a.BRID,ISBN,BName,author,abstract,total_amount,current_amount,EName,edit_ID,edit_date,price from book_info a,book_edit_info b,book_price_info c,book_date_info d,edit_house_info e where a.BRID=b.BRID and a.BRID=c.BRID and a.BRID=d.BRID and b.PHID=e.PHID;";
            MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            msda.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["ISBN"].ToString());
                myitem.SubItems.Add(dr["BName"].ToString());
                myitem.SubItems.Add(dr["author"].ToString());
                myitem.SubItems.Add(dr["EName"].ToString());
                myitem.SubItems.Add(dr["edit_ID"].ToString());
                myitem.SubItems.Add(Convert.ToDateTime(dr["edit_date"]).ToString("yyyy-MM-dd"));
                myitem.SubItems.Add(dr["total_amount"].ToString());
                myitem.SubItems.Add(dr["current_amount"].ToString());
                myitem.SubItems.Add(dr["price"].ToString());
                myitem.SubItems.Add(dr["abstract"].ToString());
                //隐藏列BRID
                myitem.SubItems.Add(dr["BRID"].ToString());
                listView1.Items.Add(myitem);
            }
        }

        private void Frm_borrow_manage_Load(object sender, EventArgs e)
        {
            lbl_mname.Text = mname;
            bind_book_info();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem myitem = listView1.SelectedItems[0];
                txt_ISBN.Text = myitem.SubItems[0].Text;
                txt_BName.Text = myitem.SubItems[1].Text;
                txt_author.Text = myitem.SubItems[2].Text;
                txt_Ename.Text = myitem.SubItems[3].Text;
                txt_edit_ID.Text = myitem.SubItems[4].Text;
                brid = int.Parse(myitem.SubItems[10].Text);
            }
        }

        protected void clearTextBox()
        {
            txt_ISBN.Text = "";
            txt_BName.Text = "";
            txt_author.Text = "";
            txt_Ename.Text = "";
            txt_edit_ID.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //实现模糊查找
            string ISBN=txt_ISBN.Text.Trim();
            string BName=txt_BName.Text.Trim();
            string author=txt_author.Text.Trim();
            string EName=txt_Ename.Text.Trim();
            string edit_ID=txt_edit_ID.Text;
            if (!(ISBN == "" && BName == "" && author == "" && EName == "" && edit_ID == ""))
            {
                if (edit_ID == "")
                {
                    MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                    conn.Open();
                    string str = string.Format("select a.BRID,ISBN,BName,author,abstract,total_amount,current_amount,EName,edit_ID,edit_date,price from book_info a,book_edit_info b,book_price_info c,book_date_info d,edit_house_info e where a.BRID=b.BRID and a.BRID=c.BRID and a.BRID=d.BRID and b.PHID=e.PHID " +
                        "and ISBN LIKE '%{0}%' AND BName LIKE '%{1}%' AND author LIKE '%{2}%' AND EName LIKE '%{3}%';", ISBN, BName, author, EName);
                    MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
                    DataTable dt = new DataTable();
                    msda.Fill(dt);
                    conn.Close();
                    listView1.Items.Clear();
                    foreach (DataRow dr in dt.Rows)
                    {
                        ListViewItem myitem = new ListViewItem(dr["ISBN"].ToString());
                        myitem.SubItems.Add(dr["BName"].ToString());
                        myitem.SubItems.Add(dr["author"].ToString());
                        myitem.SubItems.Add(dr["EName"].ToString());
                        myitem.SubItems.Add(dr["edit_ID"].ToString());
                        myitem.SubItems.Add(Convert.ToDateTime(dr["edit_date"]).ToString("yyyy-MM-dd"));
                        myitem.SubItems.Add(dr["total_amount"].ToString());
                        myitem.SubItems.Add(dr["current_amount"].ToString());
                        myitem.SubItems.Add(dr["price"].ToString());
                        myitem.SubItems.Add(dr["abstract"].ToString());
                        //隐藏列BRID
                        myitem.SubItems.Add(dr["BRID"].ToString());
                        listView1.Items.Add(myitem);
                    }
                }
                else
                {
                    int edit_id = int.Parse(edit_ID);
                    MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                    conn.Open();
                    string str = string.Format("select a.BRID,ISBN,BName,author,abstract,total_amount,current_amount,EName,edit_ID,edit_date,price from book_info a,book_edit_info b,book_price_info c,book_date_info d,edit_house_info e where a.BRID=b.BRID and a.BRID=c.BRID and a.BRID=d.BRID and b.PHID=e.PHID " +
                        "and ISBN LIKE '%{0}%' AND BName LIKE '%{1}%' AND author LIKE '%{2}%' AND EName LIKE '%{3}%' AND edit_ID={4};", ISBN, BName, author, EName, edit_id);
                    MySqlDataAdapter msda = new MySqlDataAdapter(str, conn);
                    DataTable dt = new DataTable();
                    msda.Fill(dt);
                    conn.Close();
                    listView1.Items.Clear();
                    foreach (DataRow dr in dt.Rows)
                    {
                        ListViewItem myitem = new ListViewItem(dr["ISBN"].ToString());
                        myitem.SubItems.Add(dr["BName"].ToString());
                        myitem.SubItems.Add(dr["author"].ToString());
                        myitem.SubItems.Add(dr["EName"].ToString());
                        myitem.SubItems.Add(dr["edit_ID"].ToString());
                        myitem.SubItems.Add(Convert.ToDateTime(dr["edit_date"]).ToString("yyyy-MM-dd"));
                        myitem.SubItems.Add(dr["total_amount"].ToString());
                        myitem.SubItems.Add(dr["current_amount"].ToString());
                        myitem.SubItems.Add(dr["price"].ToString());
                        myitem.SubItems.Add(dr["abstract"].ToString());
                        //隐藏列BRID
                        myitem.SubItems.Add(dr["BRID"].ToString());
                        listView1.Items.Add(myitem);
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("请先选择要借阅的书籍！");
            }
            else if (listView1.SelectedItems[0].SubItems[7].Text == "0")
            {
                lbl_note.Text = "该书在库数量为零，借阅失败！";
                lbl_note.ForeColor= Color.Red;
            }
            else
            {
                Frm_reader_info frm= new Frm_reader_info();
                frm.ShowDialog();
                string rid = frm.get_rid();
                if (rid!=null)
                {
                    MySqlConnection conn = new MySqlConnection("Data Source=localhost;database=my_db1;UID=root;PWD=123456");
                    conn.Open();
                    string str = string.Format("select state,tmp_borrow_num,borrow_limit from reader_info a,reader_type b where RID='{0}' and a.RTID=b.RTID;",rid);
                    MySqlDataAdapter msda=new MySqlDataAdapter(str, conn);
                    DataTable dt=new DataTable();
                    msda.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        DialogResult dResult0 = MessageBox.Show("输入读者号不存在！");
                    }
                    else
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dr["state"].ToString() == "1")
                            {
                                DialogResult dResult = MessageBox.Show("你有罚款未缴清，是否前往缴费？缴费后方可借阅。", "系统提示", MessageBoxButtons.YesNo);
                                if (dResult == DialogResult.Yes)
                                {
                                    Frm_fine_manage frm1 = new Frm_fine_manage(mname, mid);
                                    frm1.returnEvent += new myDelegate(Frm_returnEvent);
                                    frm1.Show();
                                    this.Hide();
                                }
                            }
                            else if (int.Parse(dr["tmp_borrow_num"].ToString()) == int.Parse(dr["borrow_limit"].ToString()))
                            {
                                lbl_note.Text = "已超过最大借阅数量！";
                                lbl_note.ForeColor = Color.Red;
                            }
                            else
                            {
                                MySqlCommand cmd = new MySqlCommand("borrowBook", conn);
                                cmd.CommandType = CommandType.StoredProcedure;
                                //定义输入函数
                                cmd.Parameters.AddWithValue("brid_", brid);
                                cmd.Parameters.AddWithValue("rid_", rid);
                                cmd.Parameters.AddWithValue("mid_", mid);
                                //定义输出函数
                                MySqlParameter result = cmd.Parameters.Add("result", MySqlDbType.Int64);
                                result.Direction = ParameterDirection.Output;
                                cmd.ExecuteNonQuery();
                                if (result.Value.ToString() == "1")
                                {
                                    lbl_note.Text = "恭喜你，借阅成功！";
                                    lbl_note.ForeColor = Color.Blue;
                                    //清空输入框
                                    clearTextBox();
                                    bind_book_info();
                                }
                                else
                                {
                                    lbl_note.Text = "对不起，借阅失败！";
                                    lbl_note.ForeColor = Color.Red;
                                }
                            }
                        }
                    }
                    conn.Close();
                }
            }
        }

        private void Frm_returnEvent()
        {
            this.Show();
            bind_book_info();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            clearTextBox();
            bind_book_info();
        }
    }
}
